package com.cskaoyan.mall.dao.mall;

import com.cskaoyan.mall.model.Admin;
import com.cskaoyan.mall.model.bo.mall.AskGoodsMsgBO;
import com.cskaoyan.mall.model.vo.admin.GetGoodsVO;
import com.cskaoyan.mall.model.vo.admin.GetSpecVO;
import com.cskaoyan.mall.model.vo.admin.OrderSpecNameVO;
import com.cskaoyan.mall.model.vo.mall.*;
import com.cskaoyan.mall.utils.DruidUtils;
import com.cskaoyan.mall.utils.StringUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class MallGoodsDaoImpl implements MallGoodsDao{
    @Override
    public List<SearchGoodsVO> searchGoods(String keyword) {
        String basesql = "select * from goods where 1=1 ";
        String param = null;
        if (!StringUtils.isEmpty(keyword)) {
            basesql = basesql + " and name like ? ";
            param = ("%" + keyword + "%");
        }
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        List<SearchGoodsVO> admins = null;
        try {
            admins = runner.query(basesql,new BeanListHandler<>(SearchGoodsVO.class),param);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return admins;
    }

    @Override
    public GetGoodsInfoVO getGoodsInfo(String id) {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        List<GetGoodsInfoSpecVO> specVOList = null;
        GetGoodsInfoVO getGoodsVO = null;
        try {
            specVOList = runner.query("select id , specName , stockNum , unitPrice from spec where goodsid = ? ",new BeanListHandler<>(GetGoodsInfoSpecVO.class),id);
            getGoodsVO = runner.query("select id , img , `name` , `desc` , typeId from goods where id = ? ",new BeanHandler<>(GetGoodsInfoVO.class),id);
            getGoodsVO.setSpecs(specVOList);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return getGoodsVO;
    }

    /*
        private Integer id;
    private String content;
    private GetGoodsMsgAskerVO asker;
    private Date time;
    private GetGoodsMsgReplyVO reply;
     */
    @Override
    public List<GetGoodsMsgVO> getGoodsMsg(String id) {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        List<GetGoodsMsgVO> getGoodsMsgVOList = null;
        GetGoodsMsgAskerVO getGoodsMsgAskerVO = null;
        GetGoodsMsgReplyVO getGoodsMsgReplyVO = null;
        GetGoodsMsgUserId getGoodsMsgUserId = null;
        Long replynum = null;
        try {
            getGoodsMsgVOList = runner.query("select id , content , createtime as time from msg where goodsId = ? ",new BeanListHandler<>(GetGoodsMsgVO.class),id);
            for (GetGoodsMsgVO getGoodsMsgVO : getGoodsMsgVOList) {
                getGoodsMsgUserId = runner.query("select userId from msg where id = ? ",new BeanHandler<>(GetGoodsMsgUserId.class),getGoodsMsgVO.getId());
                getGoodsMsgAskerVO = runner.query("select nickname from user where id = ? ",new BeanHandler<>(GetGoodsMsgAskerVO.class),getGoodsMsgUserId.getUserId());
                getGoodsMsgVO.setAsker(getGoodsMsgAskerVO.getNickname());
                replynum = (Long) runner.query("select count(replyContent) from msg where id = ? ",new ScalarHandler(),getGoodsMsgVO.getId());
                if (replynum != 0) {
                    getGoodsMsgReplyVO = runner.query("select updatetime as time , replyContent as content from msg where id = ? ",new BeanHandler<>(GetGoodsMsgReplyVO.class),getGoodsMsgVO.getId());
                }
                getGoodsMsgVO.setReply(getGoodsMsgReplyVO);
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return getGoodsMsgVOList;
    }
    /*
    token=email msg goodsId
     */
    @Override
    public int askGoodsMsg(AskGoodsMsgBO msg) {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        GetUserIdVO userId = null;
        try {
            userId = runner.query("select id from user where email = ? ",new BeanHandler<>(GetUserIdVO.class),msg.getToken());
            runner.update("insert into msg (content , goodsId , createtime , userId , state) values (?,?,Now(),?,1)",
                    msg.getMsg(),
                    msg.getGoodsId(),
                    userId.getId());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return 404;
        }
        return 200;
    }
//    {"code":0,
//            "data":{
//        "commentList":[
//        {"user":{"nickname":"GAATTC"},
//            "score":100.0,
//                "id":117,
//                "specName":"l",
//                "comment":"太棒了，好评",
//                "time":"2020-08-10 16:54:57",
//                "userId":545},
    @Override
    public GetGoodsCommentVO getGoodsComment(String id) {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        List<GetCommentVO> commentList = null;
        GetNicknameVO nickname = null;
        OrderSpecNameVO specName = null;
        //GetGoodsCommentVO result = null;
        try {
            commentList = runner.query("select score , orderId as id , content as comment , time , userId from comment where goodsId = ? ",new BeanListHandler<>(GetCommentVO.class),id);
            for (GetCommentVO comment : commentList) {
                nickname = runner.query("select nickname from user where id = ?",new BeanHandler<>(GetNicknameVO.class),comment.getUserId());
                comment.setUser(nickname);
                specName = runner.query("select spec from orders where id = ?",new BeanHandler<>(OrderSpecNameVO.class),comment.getId());
                comment.setSpecName(specName.getSpec());
//                result.setCommentList(commentList);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return new GetGoodsCommentVO(commentList);
    }
}
